<?php
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */
global $framework;
require_once("webapp/application/application.php");
require_once("$framework/paginator.php");
require_once("$framework/session.php");
require_once("webapp/managers/payment.multipage.php");
require_once("webapp/helpers/cart.helpers.php");
require_once("webapp/entities/product.entity.php");

/**
 * cart manager for this little demonstration site.
 * the <em>cartManager.php</em> is the cart handler for products to be buyed by user
 * dans displayed on <strong>cart.tpl</strong>, into the <strong>catalog</strong> and/or the "<strong>cart</strong>" page.
 * @author frederic
 *
 */
class CartManager extends Application{
	/**
	 * the currently used product.
	 * @var unknown_type
	 */
	protected $product;
	/**
	 * The cart container.
	 * @var unknown_type
	 */
	protected $cart;
	
	
	protected $paymentWizard = null;
		
	/**
	 * (non-PHPdoc)
	 * @see framework/WebApp#initialize()
	 */
	public function initialize(){
		__debug(__CLASS__."::initialize() - start",__FILE__,__LINE__);
		$this->_name="cart";
		$this->product=array();
		$this->cart = CartHelpers::get();
		CartHelpers::set($this->cart);
		$this->paymentWizard=new PaymentMultiPageWizard($this,"pay");
		
		__template_set("cart",$this->cart);
		__debug(__CLASS__."::initialize() - end",__FILE__,__LINE__);
	}
	
	/**
	 * display all product from cart.
	 * @param $type must be the product
	 * @param $id not used here.
	 * @return unknown_type
	 */
	public function lister($type,$id){
		__debug(__CLASS__."::lister() - start: type=$type,id=$id",__FILE__,__LINE__);
		__template_set("cart",$this->cart);
		__debug(__CLASS__."::lister() - end",__FILE__,__LINE__);
		return "cart/list";
	}
	
	/**
	 * Add the asked product ($type=product) with $id (=product_id) to the cart.
	 * @param $type here it must be a product type.
	 * @param $id the product_id identifier.
	 * @return template to be displayed.this->
	 */
	public function add($type,$id){
		__debug(__CLASS__."::add() - start: type=$type,id=$id",__FILE__,__LINE__);
		$request = $_REQUEST;
		$this->product = __entity($type,$id);
		__debug(__CLASS__."::add() - start: product:".print_r($product,true),__FILE__,__LINE__);
		CartHelpers::addToCart($this->product,1);
		$this->cart = CartHelpers::get();
		__template_set("cart",$this->cart);
		
		__debug(__CLASS__."::add() - end",__FILE__,__LINE__);

		return "cart/list";
	}
	
	public function delete($type,$id){
		__debug(__CLASS__."::delete() - start: type=$type,id=$id",__FILE__,__LINE__);
		$request = $_REQUEST;
		$this->product = __entity($type,$id);
		CartHelpers::removeFromCart($this->product,1);
		$this->cart = CartHelpers::get();
		__template_set("cart",$this->cart);
		
		__debug(__CLASS__."::delete() - end",__FILE__,__LINE__);

		return "cart/list";
		
	}
	
	public function clean($type, $id){	
		__debug(__CLASS__."::validate() - start: type=$type,id=$id",__FILE__,__LINE__);
		$cart=CartHelpers::get("cart");
		$cart['list']=array();
		$cart['count']=0;
		CartHelpers::set($cart);
		__debug(__CLASS__."::validate() - end",__FILE__,__LINE__);
		return "cart/list";
	}
	
	public function validate($type,$id){
		__debug(__CLASS__."::validate() - start: type=$type,id=$id",__FILE__,__LINE__);
		$cart=CartHelpers::get();
		__debug(__CLASS__."::validate() - end",__FILE__,__LINE__);
		return "cart/validate";
	}

	public function pay($type,$id){
		__debug(__CLASS__."::validate() - start: type=$type,id=$id",__FILE__,__LINE__);
		$cart=CartHelpers::get();
		$this->paymentWizard->action($type,$id);
		__template_set("wizard_template",$this->paymentWizard->getTemplate());
		__debug(__CLASS__."::validate() - end",__FILE__,__LINE__);
		return "cart/pay";
	}
	
}
?>